package com.weshare.apitest.source;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011;

import java.util.Properties;

public class SourceTest3_Kafka {

    public static void main(String[] args) throws Exception {

        // 创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        Properties properties = new Properties();
        properties.put("bootstrap.servers", "192.168.2.208:9092");
        properties.put("key-serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value-serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("auto-offset-reset", "latest");

        // 从Kafka读取数据
        DataStream<String> dataStream = env.addSource(
                new FlinkKafkaConsumer011<>("sensor", new SimpleStringSchema(), properties));

        dataStream.print();

        // 执行
        env.execute();
    }
}
